<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>零件打孔检测系统</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3B82F6',
                        secondary: '#10B981',
                        danger: '#EF4444',
                        neutral: '#6B7280',
                    },
                    fontFamily: {
                        sans: ['Inter', 'system-ui', 'sans-serif'],
                    },
                }
            }
        }
    </script>
    
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .shadow-soft {
                box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
            }
            .transition-custom {
                transition: all 0.3s ease;
            }
        }
    </style>
</head>
<body class="bg-gray-50 font-sans text-gray-800">
    <div class="container mx-auto px-4 py-8 max-w-6xl">
        <!-- 标题区域 -->
        <header class="text-center mb-10">
            <h1 class="text-[clamp(1.8rem,4vw,2.5rem)] font-bold text-gray-800 mb-2">零件打孔检测系统</h1>
            <p class="text-neutral max-w-2xl mx-auto">上传零件图片，系统将自动校正图像并检测表面打孔是否为四个，且四个孔中心是否构成正方形</p>
        </header>
        
        <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
            <!-- 左侧：上传区域 -->
            <div class="lg:col-span-1">
                <div class="bg-white rounded-xl shadow-soft p-6 h-full">
                    <h2 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fa fa-upload text-primary mr-2"></i>上传零件图片
                    </h2>
                    
                    <!-- 上传区域 -->
                    <div id="upload-area" class="border-2 border-dashed border-gray-300 rounded-lg p-8 text-center mb-6 cursor-pointer hover:border-primary transition-custom">
                        <i class="fa fa-picture-o text-5xl text-gray-400 mb-3"></i>
                        <p class="text-gray-500 mb-2">拖拽图片到此处或点击上传</p>
                        <p class="text-sm text-gray-400">支持 JPG、PNG 格式</p>
                        <input type="file" id="file-input" class="hidden" accept="image/*">
                    </div>
                    
                    <!-- 拍摄提示 -->
                    <div class="bg-blue-50 border border-blue-100 rounded-lg p-4 mb-6">
                        <h3 class="font-medium text-primary mb-2 flex items-center">
                            <i class="fa fa-lightbulb-o mr-2"></i>拍摄提示
                        </h3>
                        <ul class="text-sm text-gray-600 space-y-1">
                            <li>• 确保光线均匀，避免反光</li>
                            <li>• 零件应完整出现在画面中</li>
                            <li>• 尽量保持零件水平拍摄</li>
                            <li>• 背景应简洁，与零件有明显区分</li>
                        </ul>
                    </div>
                    
                    <!-- 操作按钮 -->
                    <button id="detect-btn" class="w-full bg-primary hover:bg-primary/90 text-white font-medium py-3 px-4 rounded-lg transition-custom flex items-center justify-center disabled:opacity-50 disabled:cursor-not-allowed" disabled>
                        <i class="fa fa-search mr-2"></i>开始检测
                    </button>
                </div>
            </div>
            
            <!-- 右侧：结果展示 -->
            <div class="lg:col-span-2">
                <!-- 图片展示区域 -->
                <div class="bg-white rounded-xl shadow-soft p-6 mb-6">
                    <h2 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fa fa-images text-primary mr-2"></i>处理结果
                    </h2>
                    
                    <!-- 处理步骤标签 -->
                    <div class="flex border-b border-gray-200 mb-4">
                        <button class="step-btn active py-2 px-4 font-medium text-sm border-b-2 border-primary text-primary" data-step="original">原始图像</button>
                        <button class="step-btn py-2 px-4 font-medium text-sm border-b-2 border-transparent text-gray-500 hover:text-gray-700" data-step="corrected">校正后</button>
                        <button class="step-btn py-2 px-4 font-medium text-sm border-b-2 border-transparent text-gray-500 hover:text-gray-700" data-step="result">检测结果</button>
                    </div>
                    
                    <!-- 图片容器 -->
                    <div class="flex justify-center items-center h-80 bg-gray-100 rounded-lg overflow-hidden mb-4">
                        <div id="original-image" class="step-content w-full h-full flex items-center justify-center">
                            <img src="https://picsum.photos/seed/part/600/400" alt="示例零件图片" class="max-w-full max-h-full object-contain p-4">
                        </div>
                        <div id="corrected-image" class="step-content w-full h-full hidden">
                            <img src="" alt="校正后的图片" class="max-w-full max-h-full object-contain p-4">
                        </div>
                        <div id="result-image" class="step-content w-full h-full hidden">
                            <img src="" alt="检测结果图片" class="max-w-full max-h-full object-contain p-4">
                        </div>
                    </div>
                    
                    <!-- 处理进度 -->
                    <div id="progress-container" class="hidden">
                        <div class="flex justify-between text-sm mb-1">
                            <span>处理中</span>
                            <span id="progress-percent">0%</span>
                        </div>
                        <div class="w-full bg-gray-200 rounded-full h-2.5">
                            <div id="progress-bar" class="bg-primary h-2.5 rounded-full" style="width: 0%"></div>
                        </div>
                    </div>
                </div>
                
                <!-- 检测结果 -->
                <div class="bg-white rounded-xl shadow-soft p-6">
                    <h2 class="text-xl font-semibold mb-4 flex items-center">
                        <i class="fa fa-check-circle text-primary mr-2"></i>检测结论
                    </h2>
                    
                    <div id="result-placeholder" class="text-center py-8 text-gray-400">
                        <i class="fa fa-file-text-o text-4xl mb-3"></i>
                        <p>上传图片并开始检测后显示结果</p>
                    </div>
                    
                    <div id="result-details" class="hidden">
                        <div class="flex items-center mb-4">
                            <div id="result-status-icon" class="w-12 h-12 rounded-full flex items-center justify-center mr-4">
                                <i class="fa fa-check text-2xl"></i>
                            </div>
                            <div>
                                <h3 id="result-title" class="text-xl font-semibold"></h3>
                                <p id="result-description" class="text-gray-600"></p>
                            </div>
                        </div>
                        
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-4 mt-6">
                            <div class="bg-gray-50 p-4 rounded-lg">
                                <h4 class="font-medium text-gray-700 mb-2">孔洞检测</h4>
                                <p id="hole-count" class="text-2xl font-semibold"></p>
                            </div>
                            <div class="bg-gray-50 p-4 rounded-lg">
                                <h4 class="font-medium text-gray-700 mb-2">正方形验证</h4>
                                <p id="square-check" class="text-2xl font-semibold"></p>
                            </div>
                        </div>
                        
                        <div class="mt-6 p-4 bg-blue-50 rounded-lg">
                            <h4 class="font-medium text-primary mb-2">详细参数</h4>
                            <ul id="detail-params" class="text-sm text-gray-600 space-y-1">
                                <!-- 参数将动态填充 -->
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <footer class="mt-12 text-center text-gray-500 text-sm">
            <p>零件打孔检测系统 - 用于OpenCV教学实践</p>
        </footer>
    </div>

    <script>
        // 上传功能
        const uploadArea = document.getElementById('upload-area');
        const fileInput = document.getElementById('file-input');
        const detectBtn = document.getElementById('detect-btn');
        const originalImage = document.getElementById('original-image').querySelector('img');
        
        uploadArea.addEventListener('click', () => {
            fileInput.click();
        });
        
        uploadArea.addEventListener('dragover', (e) => {
            e.preventDefault();
            uploadArea.classList.add('border-primary');
        });
        
        uploadArea.addEventListener('dragleave', () => {
            uploadArea.classList.remove('border-primary');
        });
        
        uploadArea.addEventListener('drop', (e) => {
            e.preventDefault();
            uploadArea.classList.remove('border-primary');
            
            if (e.dataTransfer.files.length) {
                handleFile(e.dataTransfer.files[0]);
            }
        });
        
        fileInput.addEventListener('change', () => {
            if (fileInput.files.length) {
                handleFile(fileInput.files[0]);
            }
        });
        
        function handleFile(file) {
            if (!file.type.match('image.*')) {
                alert('请上传图片文件');
                return;
            }
            
            const reader = new FileReader();
            reader.onload = (e) => {
                originalImage.src = e.target.result;
                detectBtn.disabled = false;
            };
            reader.readAsDataURL(file);
        }
        
        // 步骤切换
        const stepBtns = document.querySelectorAll('.step-btn');
        const stepContents = document.querySelectorAll('.step-content');
        
        stepBtns.forEach(btn => {
            btn.addEventListener('click', () => {
                const step = btn.dataset.step;
                
                // 更新按钮状态
                stepBtns.forEach(b => {
                    b.classList.remove('active', 'border-primary', 'text-primary');
                    b.classList.add('border-transparent', 'text-gray-500');
                });
                btn.classList.add('active', 'border-primary', 'text-primary');
                btn.classList.remove('text-gray-500', 'border-transparent');
                
                // 显示对应内容
                stepContents.forEach(content => {
                    content.classList.add('hidden');
                });
                document.getElementById(`${step}-image`).classList.remove('hidden');
            });
        });
        
        // 检测功能
        detectBtn.addEventListener('click', () => {
            // 显示进度条
            document.getElementById('progress-container').classList.remove('hidden');
            detectBtn.disabled = true;
            
            // 模拟处理进度
            let progress = 0;
            const progressBar = document.getElementById('progress-bar');
            const progressPercent = document.getElementById('progress-percent');
            
            const interval = setInterval(() => {
                progress += 5;
                progressBar.style.width = `${progress}%`;
                progressPercent.textContent = `${progress}%`;
                
                // 模拟不同处理阶段
                if (progress === 30) {
                    // 显示校正后的图片
                    document.getElementById('corrected-image').querySelector('img').src = 
                        'https://picsum.photos/seed/corrected/600/400';
                    stepBtns[1].click();
                }
                
                if (progress === 100) {
                    clearInterval(interval);
                    // 显示检测结果
                    document.getElementById('result-image').querySelector('img').src = 
                        'https://picsum.photos/seed/result/600/400';
                    stepBtns[2].click();
                    
                    // 显示结果详情
                    document.getElementById('result-placeholder').classList.add('hidden');
                    const resultDetails = document.getElementById('result-details');
                    resultDetails.classList.remove('hidden');
                    
                    // 填充结果数据
                    document.getElementById('result-status-icon').className = 
                        'w-12 h-12 rounded-full flex items-center justify-center mr-4 bg-green-100 text-green-500';
                    document.getElementById('result-title').textContent = '检测合格';
                    document.getElementById('result-description').textContent = '零件表面有4个孔，且孔中心构成标准正方形';
                    document.getElementById('hole-count').textContent = '4 个孔';
                    document.getElementById('square-check').textContent = '符合标准';
                    
                    // 填充详细参数
                    const paramsList = document.getElementById('detail-params');
                    paramsList.innerHTML = `
                        <li>• 孔直径: 5.2mm (±0.1mm)</li>
                        <li>• 边长: 25.0mm (±0.2mm)</li>
                        <li>• 对角线: 35.4mm (±0.3mm)</li>
                        <li>• 角度偏差: 0.5° (在允许范围内)</li>
                    `;
                }
            }, 100);
        });
    </script>
</body>
</html>
